PATHMac OS 8 and 9 Developer Documentation > Networking and Communications > URL Access Manager >

Transferring Files With the URL Access Manager


Downloading Data from Multiple URLs

The Downloader application downloads data from multiple URLs in a predetermined order, and stores the data in multiple files. The application obtains the URLs that it downloads by reading a text file in which the URLs have been stored.

The Downloader application's main routine sets up the application's main event loop. It calls the getURL routine to read obtain a URL from a file of URLs and is ready to start downloading data.

Listing 2-10  The Downloader application's main routine

#include <Events.h>
#include <stdio.h>
#include "URLAccess.h"
#include "string.h"
#include "Memory.h"

void main (void)
{
    OSStatus err = noErr;
    char url[255];
    int count, fileCount = 0;
    EventRecord ev;
    // Call MaxApplZone, MoreMasters.
    // Initialize graph port, fonts, menus, cursor, and dialogs.
    // Clear the screen.

    while ( url != nil ) {
        // Handle Events through each loop
        WaitNextEvent(everyEvent, &ev, 0, nil);
        eventHandler( nil, &ev );

        // Obtain a URL from the file of URLs.
        result = getURL(url); // (Routine not shown.)
        if ( result == eofErr ) { // Handle error condition. }
        // Call Download routine.
        result = DoDownload( url );
        if ( result != noErr) { // Handle error condition. }
    }
    printf("\n All of the URLs have been downloaded.\n");
}

The DoDownload routine shown in Listing 2-11 does the actual work of downloading data from the URL. It creates a file specification for the data that is to be downloaded and a URL reference. It sets the open flags to replace an existing file (if any) with the downloaded data and to display a progress indicator during the download. Then the DoDownload routine calls URLDownload to download the data.

Listing 2-11   The Downloader application's DoDownload routine

void DoDownload (void)
{
    URLReference urlRef;
    FSSpec dest, *destPtr = nil;
    destPtr = &dest;
    Handle destHandle = nil;
    int openFlags = kURLReplaceExistingFlag + kURLDisplayProgressFlag;
    Str255 newFile;
        
    // Create the file specification for the download.
    sprintf((char*)newFile, "File %d", fileCount);
    c2pstr((char*)newFile);
    fileCount++;
    err = FSMakeFSSpec(0, 0, newFile, &dest);
    // Create the URLReference.
    err = URLNewReference( theURL, &urlRef );
    if (err != noErr) printf("URLNewReference failed\n");
    // Download the data.
    err = URLDownload( urlRef, destPtr, destHandle, openFlags,
    &eventHandler, (void*)&fileCount );

    if (err != noErr) printf("URLDownload failed\n");
    // Clean up.
    err = URLDisposeReference( urlRef );
    if (err != noErr) printf("URLDisposeReference failed\n");
    return err;
}

The Downloader application uses the eventHandler routine as a general purpose event handling routine. In this example, the user context ( context ) is an integer value.

Listing 2-12  The Downloader application's system event callback routine

pascal long eventHandler( void * userContext, EventRecord* eventPtr )
{
    EventRecord* ev;
    int what = 0;
    int context = 0;
    int* intPtr = nil;
// Convert the event pointer into an event record.
    ev = (EventRecord*)eventPtr;
    what = ev->what;
// Convert the void* to an integer.
    intPtr = (int*)userContext;
    context = *intPtr;
    if (context < 0 || context > 99)
        context = -1; // Unknown context
switch (what) {
    case 0 : // Null Event
        break;
    case mouseDown:
        printf("Handler Called: mouseDown User Context: %d\n", context);
        // Call routine to handle event.
        break;
    case updateEvt:
        printf("Handler Called: updateEvt User Context: %d\n", context);
        // Call routine to handle event.
        break;
    case activateEvt:
        printf("Handler Called: activateEvt User Context: %d\n",
            context);
        // Call routine to handle event.
        break;
    case keyDown:
        printf("Handler Called: keyDown User Context: %d\n", context);
        // Call routine to handle event.
        break;
    default:
        printf("Handler Called: Default User Context: %d\n", context);
        break;
    }
    return nil;
}


© 1999 Apple Computer, Inc. – (Last Updated 07 May 99)